package com.jiaai.util;

import com.alibaba.fastjson.JSONObject;
import comm.utils.pay.comm.util.HttpClientUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * class_name:WeiBoUtil
 * describe:------------------>微博相关
 * create_user:张昉平
 * create_date:2018/2/1
 * create_time:13:47
 */
public class WeiBoUtil {

    private static final String ACCESS_TOKEN = "https://api.weibo.com/oauth2/access_token";//获取授权
    private static final String USER_SHOW = "https://api.weibo.com/2/users/show.json";//获取用户信息

    public static void main(String[] args) {
        System.out.println( getUserInfo("123213", "123213").toJSONString());
    }

    public static JSONObject getUserInfo(String access_token, String uid){
        CloseableHttpClient client = null;
        try {
            client = HttpClientUtil.getSSLClient();

            String finalUrl = USER_SHOW.concat("?access_token=")
                    .concat(access_token)
                    .concat("&uid=").concat(uid);

            HttpGet get = new HttpGet(finalUrl);

            CloseableHttpResponse resp = client.execute(get);
            InputStream in = resp.getEntity().getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(in, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuilder buffer = new StringBuilder();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }

            bufferedReader.close();
            inputStreamReader.close();
            in.close();
            return JSONObject.parseObject(buffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
